package zqservlet;

import net.sf.json.JSONObject;
import zqtools.DBUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

//--专门针对华壹的设备写的servlet
@WebServlet(name = "hy2Servlet", urlPatterns = "/hy2")
public class hy2Servlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf8");

        StringBuilder sbr = new StringBuilder();
        try (BufferedReader reader = request.getReader()) {
            String line;
            while ((line = reader.readLine()) != null) {
                sbr.append(line).append('\n');
            }
            if (sbr.length() > 1) {
                sbr.replace(sbr.length() - 1, sbr.length(), "");
            }
        } catch (IOException e) {
            System.out.println(("RequestUtil,IOException:" + e));
        }
        String str = sbr.toString();
        JSONObject of = JSONObject.fromObject(str);

        //--前端对应的PC版本的URLSearchParams形式传递参数
//        JSONObject of = new JSONObject();
//        Enumeration<String> parameterNames = request.getParameterNames();
//        while (parameterNames.hasMoreElements()) {
//            String s = parameterNames.nextElement();
//            of.put(s, request.getParameter(s));
//        }

        JSONObject j = DBUtil.f_hy2(of);

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print(j);
        j.clear();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }
}
